package com.autologistics.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.autologistics.model.Disparter;
import com.autologistics.model.Ordertable;

public class DisparterDaoImpl  implements DisparterDao{
	HibernateTemplate hibTemplate;

	public HibernateTemplate getHibTemplate() {
		return hibTemplate;
	}

	public void setHibTemplate(HibernateTemplate hibTemplate) {
		this.hibTemplate = hibTemplate;
	}
	/**
	 * @return ����ȫ��������Ա�б� or null
	 * @author cl
	 */
	public List<Disparter> listAll(){
		String hql = "from Disparter ";
		List<Disparter> list = hibTemplate.find(hql);
		if (!list.isEmpty()){
			return list;
		}else{
			return null;
		}
	}
	public List<Disparter> listByState(int state){
		String hql = "from Disparter where state=?";
		List<Disparter> list = hibTemplate.find(hql,state);
		if (!list.isEmpty()){
			return list;
		}else{
			return null;
		}
	}
	/**
	 * ���������Ա
	 * @param dp
	 * @author cl
	 */
	public void add(Disparter dp){
		hibTemplate.save(dp);
	}
	
	/**
	 * @param id
	 * @return ����Ա
	 * @author cl
	 */
	public Disparter getById(int id){
		String hql ="from Disparter where id=?";
		List<Disparter> list = hibTemplate.find(hql,id);
		if (!list.isEmpty()){
			return list.get(0);
		}else{
			return null;
		}
	}
	/**
	 * ��������Ա
	 * @param dp
	 * @author cl
	 */
	public void update(Disparter dp){
		hibTemplate.update(dp);
	}

	@Override
	public Disparter getByCardNumber(Integer cardNumber) {
		// TODO Auto-generated method stub
		String hql ="from Disparter where idCardNum=?";
		List<Disparter> list = hibTemplate.find(hql,cardNumber);
		if (!list.isEmpty()){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	public List<Disparter> queryForOnePage(final int offset, final int length){
		String hql = "from Disparter";
		Session session = hibTemplate.getSessionFactory().openSession();
		Query q = session.createQuery(hql);  
	    q.setFirstResult(offset);  
	    q.setMaxResults(length); 
	    List<Disparter> list=q.list();
	    session.close();
		if (!list.isEmpty()){
			return list;
		}else{
			return null;
		}
	}
	
	
}
